//
// GPSMapEdit
// (c) Konstantin Galichsky (kg@geopainting.com), 2002-2007
//
// 'Options' dialog, 'View' page.
//

# include "StdAfx.h"
# include "Resource.h"
# include "Options.h"
# include "Settings.h"
# include "MapView.h"
# include "SelectColor.h"
# include "DrawAddress.h"
# include "DrawNodes.h"
# include "Button.h"
# include "Globals.h"

static
void _SaveCheckbox (HWND _hWnd, UINT _uiCmd, bool & _b, const char * _strSetting) {
	_b = ::IsDlgButtonChecked (_hWnd, _uiCmd) == BST_CHECKED;
	SaveSetting (_strSetting, _b);
}

INT_PTR CALLBACK _OptionsViewProc (HWND _hWnd, UINT _uMsg, WPARAM _wParam, LPARAM _lParam) {
	switch (_uMsg) {
		case WM_INITDIALOG:
			// Map background.
			ListView_SetBkColor (::GetDlgItem (_hWnd, IDC_COLOR_BG), GetBackgroundColor ());

			// Transparent polygons.
			SetButtonIcon (_hWnd, IDC_TPV_HATCHED, IDI_HATCHED);
			SetButtonIcon (_hWnd, IDC_TPV_HOLLOW,  IDI_HOLLOW);
			if (GetTransparentPolygonFill ())
				::CheckRadioButton (_hWnd, IDC_TPV_HATCHED, IDC_TPV_HATCHED, IDC_TPV_HATCHED);
			else
				::CheckRadioButton (_hWnd, IDC_TPV_HOLLOW,  IDC_TPV_HOLLOW,  IDC_TPV_HOLLOW);

			// Track.
			ListView_SetBkColor (::GetDlgItem (_hWnd, IDC_TRACK_COLOR), GetDefaultTrackColor ());
			::SetDlgItemInt (_hWnd, IDC_TRACK_WIDTH, GetDefaultTrackWidth (), FALSE);

			// Route.
			ListView_SetBkColor (::GetDlgItem (_hWnd, IDC_ROUTE_COLOR), GetDefaultRouteColor ());
			::SetDlgItemInt (_hWnd, IDC_ROUTE_WIDTH, GetDefaultRouteWidth (), FALSE);

			// Map infotips.
			::CheckDlgButton (_hWnd, IDC_SHOW_TYPE,    g_bTTShowType            ? BST_CHECKED : BST_UNCHECKED);
			::CheckDlgButton (_hWnd, IDC_SHOW_STREET,  g_bTTShowStreetAndNumber ? BST_CHECKED : BST_UNCHECKED);
			::CheckDlgButton (_hWnd, IDC_SHOW_CITY,    g_bTTShowCity            ? BST_CHECKED : BST_UNCHECKED);
			::CheckDlgButton (_hWnd, IDC_SHOW_ZIPCODE, g_bTTShowZipCode         ? BST_CHECKED : BST_UNCHECKED);
			::CheckDlgButton (_hWnd, IDC_SHOW_PHONE,   g_bTTShowPhone           ? BST_CHECKED : BST_UNCHECKED);
			::CheckDlgButton (_hWnd, IDC_SHOW_COMMENT, g_bTTShowComment         ? BST_CHECKED : BST_UNCHECKED);

			return TRUE;

		case WM_COMMAND: {
			switch (LOWORD (_wParam)) {
				case IDC_SELECT_COLOR_BG: {
					const HWND hColor = ::GetDlgItem (_hWnd, IDC_COLOR_BG);
					COLORREF cr = ListView_GetBkColor (hColor);
					if (SelectColor (_hWnd, cr)) {
						ListView_SetBkColor (hColor, cr);
						::InvalidateRect (hColor, NULL, TRUE);
						::UpdateWindow (hColor);

						PropSheet_Changed (::GetParent (_hWnd), _hWnd);
					}

					return TRUE;
				}

				case IDC_TPV_HATCHED:
					if (! GetTransparentPolygonFill ())
						PropSheet_Changed (::GetParent (_hWnd), _hWnd);
					break;

				case IDC_TPV_HOLLOW:
					if (GetTransparentPolygonFill ())
						PropSheet_Changed (::GetParent (_hWnd), _hWnd);
					break;

				case IDC_TRACK_SELECT_COLOR: {
					const HWND hColor = ::GetDlgItem (_hWnd, IDC_TRACK_COLOR);
					COLORREF cr = ListView_GetBkColor (hColor);
					if (SelectColor (_hWnd, cr)) {
						ListView_SetBkColor (hColor, cr);
						::InvalidateRect (hColor, NULL, TRUE);
						::UpdateWindow (hColor);

						PropSheet_Changed (::GetParent (_hWnd), _hWnd);
					}
					return TRUE;
				}

				case IDC_ROUTE_SELECT_COLOR: {
					const HWND hColor = ::GetDlgItem (_hWnd, IDC_ROUTE_COLOR);
					COLORREF cr = ListView_GetBkColor (hColor);
					if (SelectColor (_hWnd, cr)) {
						ListView_SetBkColor (hColor, cr);
						::InvalidateRect (hColor, NULL, TRUE);
						::UpdateWindow (hColor);

						PropSheet_Changed (::GetParent (_hWnd), _hWnd);
					}
					return TRUE;
				}

				case IDC_TRACK_WIDTH:
				case IDC_ROUTE_WIDTH:
					switch (HIWORD (_wParam)) {
						case EN_CHANGE:
							PropSheet_Changed (::GetParent (_hWnd), _hWnd);
							return TRUE;
					}
					break;

				case IDC_SHOW_TYPE:
				case IDC_SHOW_STREET:
				case IDC_SHOW_CITY:
				case IDC_SHOW_ZIPCODE:
				case IDC_SHOW_PHONE:
				case IDC_SHOW_COMMENT:
					PropSheet_Changed (::GetParent (_hWnd), _hWnd);
					return TRUE;

				case IDC_COLORS_ADDRESS:
					if (AdjustColorsForAddressHighlighting (_hWnd)) {
						Refresh ();
					}
					break;

				case IDC_COLORS_NODES:
					if (AdjustColorsForRoutingNodes (_hWnd)) {
						Refresh ();
					}
					break;
			}
			break;
		}

		case WM_NOTIFY: {
			const NMHDR * const pInfo = reinterpret_cast<const NMHDR *> (_lParam);
			switch (pInfo->code) {
				case PSN_APPLY: {
					// Map background.
					SaveSetting ("Color_BG",   ListView_GetBkColor (::GetDlgItem (_hWnd, IDC_COLOR_BG)));

					// Transparent polygons.
					SaveSetting ("TransparentPolygonFill", ::IsDlgButtonChecked (_hWnd, IDC_TPV_HATCHED) == BST_CHECKED);

					// Track color.
					SaveSetting ("TrackColor", ListView_GetBkColor (::GetDlgItem (_hWnd, IDC_TRACK_COLOR)));

					// Track width.
					size_t cTrackWidth = ::GetDlgItemInt (_hWnd, IDC_TRACK_WIDTH, NULL, FALSE);
					if (cTrackWidth == 0)
						cTrackWidth = 3;
					if (cTrackWidth > 10)
						cTrackWidth = 10;
					SaveSetting ("TrackWidth", (DWORD) cTrackWidth);

					// Route color.
					SaveSetting ("RouteColor", ListView_GetBkColor (::GetDlgItem (_hWnd, IDC_ROUTE_COLOR)));

					// Route width.
					size_t cRouteWidth = ::GetDlgItemInt (_hWnd, IDC_ROUTE_WIDTH, NULL, FALSE);
					if (cRouteWidth == 0)
						cRouteWidth = 3;
					if (cRouteWidth > 10)
						cRouteWidth = 10;
					SaveSetting ("RouteWidth", (DWORD) cRouteWidth);

					// Map infotips.
					_SaveCheckbox (_hWnd, IDC_SHOW_TYPE,    g_bTTShowType,            "TTShowType");
					_SaveCheckbox (_hWnd, IDC_SHOW_STREET,  g_bTTShowStreetAndNumber, "TTShowStreetAndNumber");
					_SaveCheckbox (_hWnd, IDC_SHOW_CITY,    g_bTTShowCity,            "TTShowCity");
					_SaveCheckbox (_hWnd, IDC_SHOW_ZIPCODE, g_bTTShowZipCode,         "TTShowZip");
					_SaveCheckbox (_hWnd, IDC_SHOW_PHONE,   g_bTTShowPhone,           "TTShowPhone");
					_SaveCheckbox (_hWnd, IDC_SHOW_COMMENT, g_bTTShowComment,         "TTShowComment");

					Refresh ();

					break;
				}
			}
			break;
		}
	}

	return FALSE;
}

COLORREF GetBackgroundColor () {
	return GetSetting ("Color_BG", (DWORD) c_colWhite);
}

bool GetTransparentPolygonFill () {
	return GetSetting ("TransparentPolygonFill", true);
}

COLORREF GetDefaultTrackColor () {
	return GetSetting ("TrackColor", (DWORD) c_colGray);
}

size_t GetDefaultTrackWidth () {
	return GetSetting ("TrackWidth", (DWORD) 3);
}

COLORREF GetDefaultRouteColor () {
	return GetSetting ("RouteColor", (DWORD) c_colBlue);
}

size_t GetDefaultRouteWidth () {
	return GetSetting ("RouteWidth", (DWORD) 3);
}
